<template>
  <div class="customerBom">
    <div style="display: flex">
      <jc-button
        :btn-arr="['reload', 'newy', 'option']"
        :disab-arr="['close', 'cancel', 'copy', 'using', 'freeze', 'com1', 'com2', 'chart']"
        @newly="newly"
        @refresh="getUserList"
        @toExcel="handleExport"
      />
    </div>
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="searchMaterialList"
        />
        <el-button size="mini" type="primary" class="btn" @click="searchMaterialList">
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="searchMaterialList"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        serial
        v-loading="loading"
        sortable
        :table-data="tableData"
        :table-header="tableHeader"
      >
        <template v-slot:billSlot="col">
          <el-link
            type="primary"
            @click="editImpower(col.scope.row)"
          >
            {{ col.scope.row.fname }}
          </el-link>
        </template>
        <template v-slot:tagSlot="col">
          <el-button size="mini" type="warning" @click="editImpower(col.scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="delImpower(col.scope.row)">删除</el-button>
        </template>
      </jc-table>
    </div>
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="size"
        @pagination="getUserList"
      />
    </div>
    <el-dialog
      v-dialogDrag
      :title="staff.fid ? '修改' : '新增'"
      model
      :visible.sync="isMaterDialog"
      :close-on-click-modal="false"
      width="500px"
    >
      <el-form :model="staff" label-width="100px">
        <el-form-item label="姓名">
          <el-input v-model="staff.fname" />
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="staff.deptname">
            <i
              slot="suffix"
              class="el-input__icon el-icon-search"
              @click="departVisiblit=true"
            />
          </el-input>
        </el-form-item>
        <el-form-item label="车间组别">
          <el-select v-model="staff.fgroupid">
            <el-option v-for="item in workshop" :key="item.fid" :label="item.fname" :value="item.fid" />
          </el-select>
        </el-form-item>
        <el-form-item label="岗位">
          <el-input v-model="staff.fjob" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="staff.fmemo" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isMaterDialog=false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="confirmUp">{{ $t('btn.confirm') }}</el-button>
      </span>
    </el-dialog>
    <!--  部门-->
    <unit v-if="departVisiblit" msg="SX_V_WORKSHOP" @unit="selUnit" />
  </div>
</template>

<script>
import {
  deletetJxTempStaff,
  querytJxTempStaff, savetJxTempStaff, updatetJxTempStaff
} from '@/api/basicManagement/customerBom'
import search from '@/components/Search/index'
import searData from '@/components/Search/mixin'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { queryGroupByDeptID } from '@/api/resources'

export default {
  components: {
    search,
    filtration,
    jcButton,
    unit: () => import('@/components/unit')
  },
  mixins: [searData],
  data() {
    return {
      isMaterDialog: false,
      departVisiblit: false,
      workshop: [], // 组别
      staff: {},
      ftype: 103,
      total: 0, // 总条目
      pageNum: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableData: [], // 列表数据
      // 表头数据
      tableHeader: [
        { label: '姓名', prop: 'fname', width: 200 },
        { label: '部门', prop: 'deptname', width: 200 },
        { label: '车间组别', prop: 'groupname', width: 200 },
        { label: '岗位', prop: 'fjob', width: 200 },
        { label: '备注', prop: 'fmemo', width: 200 },
        { label: '操作', type: 'tag', width: 250 }
      ],
      indexfid: '',
      fname1: '',
      tableHeader1: []
    }
  },
  created() {
    this.getUserList()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '临时工列表')
        } else {
          querytJxTempStaff({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '临时工列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 客户物料对应表列表
    async getUserList(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.pageNum,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const { data: res } = await querytJxTempStaff(DATA)
      this.tableData = res.list
      this.total = res.total
      this.loading = false
    },
    // 搜索物料
    searchMaterialList(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.getUserList(ev.detail)
      } else {
        this.getUserList()
      }
    },
    // 部门
    selUnit(ev) {
      if (ev?.fid) {
        this.staff.deptname = ev.fname
        this.staff.fdeptid = ev.fid
        this.queryworkshop(ev.fid)
      }
      this.departVisiblit = false
    },
    // 组别workshop
    queryworkshop(deptId) {
      queryGroupByDeptID({ deptId }).then(res => {
        if (res.code === 0) {
          this.workshop = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 新增
    newly() {
      this.staff = {
        fname: '',
        fdeptid: '',
        fgroupid: '',
        fjob: '',
        fmemo: ''
      }
      this.isMaterDialog = true
    },
    // 修改
    editImpower(ev) {
      this.staff = ev
      this.isMaterDialog = true
    },
    // 新增 修改SPU
    confirmUp() {
      const api = this.staff.fid ? updatetJxTempStaff : savetJxTempStaff
      api(this.staff).then(res => {
        if (res.code === 0) {
          this.getUserList()
          this.isMaterDialog = false
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 删除
    delImpower(ev) {
      this.$confirm('确定要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetJxTempStaff({ fid: ev.fid }).then(res => {
          if (res.code === 0) {
            this.getUserList()
            this.$message({ showClose: true, message: res.message, type: 'success' })
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>

<style scoped lang="scss">
.customerBom {
  @include listBom;
  .table-content {
    margin-top: 5px;
  }
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
